package com.mdxq.mall.sale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mdxq.mall.response.R;
import com.mdxq.mall.sale.client.ProductServiceClient;
import com.mdxq.mall.vo.ProductBaseInfoVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.sale.entity.FlashPromotionProductRelation;
import com.mdxq.mall.sale.service.FlashPromotionProductRelationService;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 商品限时购与商品关系表 控制层。
 *
 * @author zdy
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/flashProductRelation")
public class FlashPromotionProductRelationController {

    @Autowired
    private FlashPromotionProductRelationService flashPromotionProductRelationService;

    /**
     * 添加商品限时购与商品关系表。
     *
     * @param flashPromotionProductRelation 商品限时购与商品关系表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody FlashPromotionProductRelation flashPromotionProductRelation) {
        return flashPromotionProductRelationService.save(flashPromotionProductRelation);
    }

    /**
     * 根据主键删除商品限时购与商品关系表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Long id) {
        return flashPromotionProductRelationService.removeById(id);
    }

    /**
     * 根据主键更新商品限时购与商品关系表。
     *
     * @param flashPromotionProductRelation 商品限时购与商品关系表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody FlashPromotionProductRelation flashPromotionProductRelation) {
        return flashPromotionProductRelationService.updateById(flashPromotionProductRelation);
    }

    /**
     * 查询所有商品限时购与商品关系表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<FlashPromotionProductRelation> list() {
        return flashPromotionProductRelationService.list();
    }

    /**
     * 根据商品限时购与商品关系表主键获取详细信息。
     *
     * @param id 商品限时购与商品关系表主键
     * @return 商品限时购与商品关系表详情
     */
    @GetMapping("getInfo/{id}")
    public FlashPromotionProductRelation getInfo(@PathVariable Long id) {
        return flashPromotionProductRelationService.getById(id);
    }

    @Autowired
    private ProductServiceClient productServiceClient;
    /**
     * 分页查询商品限时购与商品关系表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<FlashPromotionProductRelation> page,Long flashPromotionId,Long flashPromotionSessionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("flash_promotion_id",flashPromotionId);
        queryWrapper.eq("flash_promotion_session_id",flashPromotionSessionId);
        Page<FlashPromotionProductRelation> pageInfo = flashPromotionProductRelationService.page(page,queryWrapper);
        for (FlashPromotionProductRelation relation : pageInfo.getRecords()) {
            Object object = productServiceClient.getBaseInfo(relation.getProductId()).getData();
            relation.setProduct(BeanUtil.copyProperties(object, ProductBaseInfoVo.class));
        }
        return R.ok(pageInfo);
    }

}
